package GUI.Cocina;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.IngredientVO;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.widgets.TableColumn;

public class AgregarAlStock extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Table table_1;
	private Button btnIngresarAlSistema;
	private TableColumn tblclmnNewColumn;
	private TableColumn tblclmnNewColumn_1;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AgregarAlStock(Shell parent, int style) {
		super(parent, style);
		setText("Agregar Ingredientes Al Stock");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(669, 367);
		shell.setText(getText());
		shell.setLayout(null);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(15, 31, 245, 263);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(241);
		tblclmnNewColumn.setText("");
		
		table_1 = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table_1.setBounds(426, 31, 228, 263);
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		
		tblclmnNewColumn_1 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_1.setWidth(227);
		tblclmnNewColumn_1.setText("");
		
		Button btnYaEstaEn = new Button(shell, SWT.NONE);
		btnYaEstaEn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table.getSelection()[0];
				TableItem tableItem = new TableItem(table_1, SWT.NONE);
				tableItem.setText(t.getText());
				table.remove(table.getSelectionIndex());
				
			}
		});
		btnYaEstaEn.setBounds(275, 62, 136, 28);
		btnYaEstaEn.setText("Ya Esta En Stock");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table_1.getSelection()[0];
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(t.getText());
				table_1.remove(table_1.getSelectionIndex());
			}
		});
		btnNewButton.setBounds(275, 96, 136, 28);
		btnNewButton.setText("Cancelar");
		
		btnIngresarAlSistema = new Button(shell, SWT.NONE);
		btnIngresarAlSistema.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] faltan = table_1.getItems();
				IngredientVO [] ings= null;
				ArticleProxyMgt o = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					ings= o.getAllIngredients();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				ArrayList<IngredientVO> lista = new ArrayList<IngredientVO>();
				for(int i=0;i<faltan.length;i++){
					for (int j=0;j<ings.length;j++){
						if(faltan[i].getText().equalsIgnoreCase(ings[j].getIngr())){
							lista.add(ings[j]);
							
						}
					}
					
				}
				
				try {
					o.addInStock(lista);
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				shell.dispose();
			}
			
		});
		btnIngresarAlSistema.setBounds(261, 307, 136, 28);
		btnIngresarAlSistema.setText("Ingresar al Sistema");
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		IngredientVO[] ings=null;
		try {
			ings = uso.getOutStockIngredients();
		} catch (RemoteException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (ProxyException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		}
		for(int i=0;i<ings.length;i++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(ings[i].getIngr());
		}
		

	}

	}

